﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginInterface;
using Buccontrols;

namespace Buccaneer
{
    public class OpenFileAction : PluginAction
    {
        public OpenFileAction()
        {
            base.name = "openfile";
            base.func = new ActionFunction(OpenFile);
            base.pars.Add("filename", "the name of the file to open");
            base.pluginDaddy = MainProgram.MainForm;
            base.exceptions.Add("System.Collections.Generic.KeyNotFoundException", new ActionFunction(this.PluginNotFound));
        }

        public void OpenFile(string[] args)
        {
            string plugin = Options.GetAssociatedPlugin(args[0]);
            MainProgram.selectedPlugin = MainProgram.Plugins.LoadPlugin(plugin);

            if (MainProgram.selectedPlugin != null)
            {
                //MainProgram.MainForm.AddPlugin(MainProgram.selectedPlugin.Instance);
                MainProgram.selectedPlugin.Instance.Daddy = MainProgram.MainForm;
                MainProgram.MainForm.AddTab(MainProgram.selectedPlugin);
                MainProgram.selectedPlugin.Instance.MainInterface.Name = MainProgram.selectedPlugin.Name;
                MainProgram.Plugins.Act("open", args);
                Options.WriteComment("(i) Opened file " + args[0] + " with " + MainProgram.selectedPlugin.Name);
                MainProgram.Plugins.SwitchToPlugin(MainProgram.selectedPlugin.Name);
            }
        }

        public void PluginNotFound(string[] args)
        {
            Options.WriteComment("(x) Plugin " + Options.GetAssociatedPlugin(args[0]) + ", associated with " + Options.GetAssociatedFiletype(args[0]) + " files, was not found!");
        }
    }
}
